<template>
  <avue-crud :class="type=='view'?'top15':''" :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form" @row-update="addUpdate" @row-del="rowDel" @row-save="rowSave" @refresh-change="refreshChange" @on-load="onLoad">
    <template slot="menuLeft">
      <el-button type="text" icon="el-icon-plus" class="rightbtn" @click="addRow">新增</el-button>
    </template>
  </avue-crud>
</template>
<script>
import { numparZReg, numParseReg, codeReg } from '@/util/zhengze';
import { init } from '@/mixins/init';
import { getTemplate } from '@/api/projectplan/template';
export default {
  mixins: [init],
  props: {
    projectId: {
      type: String,
    },
    productId: {
      type: String,
    },
    materialName: {
      type: String,
    },
    type: {
      type: String,
    },
    sopform: {
      type: Object,
    },
    data: {
      type: Array,
    },
    taskStartDate: {
      type: String,
    },
    taskEndDate: {
      type: String,
    },
  },
  data() {
    return {
      form: {},
      procedureOptions: [],
      customerDrawingNo: '',
      customerName: '',
      produceShopName: '',
      equipmentList: [],
      option: {
        index: true,
        indexLabel: '序号',
        menuWidth: 130,
        addBtn: false,
        editBtn: false,
        addRowBtn: false,
        cellBtn: true,
        column: [
          {
            label: '规格',
            prop: 'specifications',
            maxlength: 30,
            overHidden: true,
          },
          {
            label: '型号',
            prop: 'model',
            maxlength: 30,
            overHidden: true,
          },
          {
            label: '生产数量',
            prop: 'purchaseNum',
            minWidth: 120,
            type: 'number',
            minRows: 0,
            maxRows: 99999,
            precision: 0,
            cell: true,
            // rules: [{ type: 'number', required: true, message: '请输入生产数量', trigger: 'blur' }],
          },
          {
            label: '计划开始日期',
            prop: 'planStartDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            rules: [
              {
                required: true,
                message: '请选择计划开始日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划结束日期',
            prop: 'planEndDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            rules: [
              {
                required: true,
                message: '请选择计划结束日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '实际开始日期',
            prop: 'actualStartDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '实际完成日期',
            prop: 'actualEndDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '首次交样日期',
            prop: 'firstSampleDate',
            hide: true,
            addDisplay: false,
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            cell: true,
            minWidth: 150,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '样件认可日期',
            prop: 'prototypeApprovalDate',
            hide: true,
            type: 'date',
            cell: true,
            valueFormat: 'yyyy-MM-dd',
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            minWidth: 150,
            addDisplay: false,
          },
          {
            label: '提交PPAP日期',
            prop: 'commitPpapDate',
            hide: true,
            cell: true,
            addDisplay: false,
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            minWidth: 150,
          },
          {
            label: 'PPAP认可日期',
            prop: 'ppapApprovalDate',
            hide: true,
            cell: true,
            addDisplay: false,
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            minWidth: 150,
          },
          {
            label: '计划发货周期',
            prop: 'planDeliverCycle',
            hide: true,
            // slot: true,
            cell: true,
            append: '月',
            minWidth: 120,
            maxlength: 3,
            type: 'number',
            minRows: 0,
            maxRows: 999,
            precision: 0,
          },
          {
            label: '实际发货周期',
            prop: 'actualDeliverCycle',
            hide: true,
            append: '月',
            cell: true,
            minWidth: 120,
            type: 'number',
            minRows: 0,
            maxRows: 999,
            precision: 0,
          },
        ],
      },
    };
  },
  created() {
    if (this.type == 'edit') {
      this.option.header = true;
      this.option.menu = true;
    } else {
      this.option.header = false;
      this.option.menu = false;
    }
    getTemplate({ executeType: '8' }).then((res) => {
      let data = res.data.data;
      if (JSON.stringify(data) == '{}') {
        this.$message.warning('您还未创建该类型计划模板');
      } else {
        data.columnList.forEach((item) => {
          this.option.column.every((col, index) => {
            if (col.prop === item) {
              this.option.column[index].hide = false; //显示列
              this.option.column[index].display = true; //新增编辑弹框显示
              return false;
            } else {
              return true;
            }
          });
        });
      }
    });
    this.findObject(this.option.column, 'planStartDate').pickerOptions.disabledDate = (time) => {
      return (
        time.getTime() < new Date(this.taskStartDate).getTime() - 86400000 ||
        time.getTime() > new Date(this.taskEndDate).getTime()
      );
    };
    this.findObject(this.option.column, 'planEndDate').pickerOptions.disabledDate = (time) => {
      return (
        time.getTime() < new Date(this.taskStartDate).getTime() - 86400000 ||
        time.getTime() > new Date(this.taskEndDate).getTime()
      );
    };
    this.findObject(this.option.column, 'actualStartDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'actualEndDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'firstSampleDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'prototypeApprovalDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'commitPpapDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'ppapApprovalDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
  },
  watch: {},
  methods: {
    addRow() {
      this.$refs.crud.rowCellAdd({
        specifications: this.sopform.specifications,
        model: this.sopform.model,
        purchaseNum: 1,
      });
    },
    rowSave(row, done, loading) {
      if (!numParseReg.test(row.purchaseNum)) {
        this.$message.error('生产数量请输入大于0的整数');
        loading();
        return;
      }
      if (row.planStartDate && row.planEndDate && new Date(row.planStartDate) > new Date(row.planEndDate)) {
        this.$message.error('计划开始日期不应晚于计划结束日期');
        loading();
        return;
      }
      if (row.actualStartDate && row.actualEndDate && new Date(row.actualStartDate) > new Date(row.actualEndDate)) {
        this.$message.error('实际开始日期不应晚于实际结束日期');
        loading();
        return;
      }
      this.data.push(row);
      done();
    },
    addUpdate(row, index, done, loading) {
      if (!numParseReg.test(row.purchaseNum)) {
        this.$message.error('生产数量请输入大于0的整数');
        loading();
        return;
      }
      if (row.planStartDate && row.planEndDate && new Date(row.planStartDate) > new Date(row.planEndDate)) {
        this.$message.error('计划开始日期不应晚于计划结束日期');
        loading();
        return;
      }
      if (row.actualStartDate && row.actualEndDate && new Date(row.actualStartDate) > new Date(row.actualEndDate)) {
        this.$message.error('实际开始日期不应晚于实际结束日期');
        loading();
        return;
      }
      this.data.splice(index, 1, row);
      done();
    },
    rowDel(row, index) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.data.splice(index, 1);
      });
    },
  },
};
</script>
<style lang="scss">
.top15 {
  margin-top: 15px;
}
</style>